package com.cardbaobao.cardbabyclient.utils;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.widget.banner.BannerView;

/**
 * findViewById
 * Created by zad
 * 2016/12/16 14:55
 */

public class ViewUtil {
    public static  <T extends View> T getView(View mConvertView, int viewId) {
        return (T) mConvertView.findViewById(viewId);
    }

    /**切换输入输入类型 文本/密码显示
     * @param editText 要改变输入类型的输入框
     * @param password 是否以密码框形式展示
     */
    public static void toggleEditTextInputType(EditText editText, boolean password){
        if(editText!=null){
            if(password){
                editText.setInputType(
                        InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }else{
                editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            }
        }
    }

    /**
     * 配置recycleView，使其在nestScrollView中滑动顺畅
     * @param recyclerView RecyclerView
     * @param manager RecyclerView.LayoutManager
     */
    public static void configRecycleViewInScrollView(RecyclerView recyclerView,
                                                     RecyclerView.LayoutManager manager){
        if(recyclerView==null||manager==null){
            return;
        }
        manager.setAutoMeasureEnabled(true);
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(manager);
    }

    /**
     * 配置轮播图，使其实现每屏展示3条数据，且两边的数据有一定的缩放效果
     * @param context context，用于获取屏幕、计算dp等
     * @param bannerView bannerView
     */
    public static void configBannerView(Context context,BannerView bannerView){
        if(context==null||bannerView==null){
            return;
        }
        //计算viewPager宽高 宽高比为59/33
        ViewGroup.LayoutParams params=bannerView.getViewPager().getLayoutParams();
        final int screenWidth=ScreenUtil.getScreenWidth();
        params.width=screenWidth-DensityUtil.dp2px(20)*4;
        params.height=(int)((screenWidth*33)/59.0f);
        bannerView.getViewPager().setLayoutParams(params);
        bannerView.setClipChildren(false);
        bannerView.getViewPager().setPageMargin(DensityUtil.dp2px(20));
        bannerView.setIndicatorVisible(false);
        bannerView.setDelayedTime(Constant.BANNER_INTERVAL_TIME);
    }
}
